'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' $Logfile: /PHSRAG/Applications/WebApplications/Insight/Common/Libraries/Utilities/SourceSafe/ReportDiffs.vbs $
' $Author: Cjr20 $
' $Revision: 3 $
' $Modtime: 1/07/09 3:54p $
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description: ReportDiffs (optionally recursively) from a specified 
'              starting project.  
' Parameters:  None.  All are specified through prompts.
' Output:      None.
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit

On Error Resume Next

Dim sSSEXEPath, sStartProject, sSSPath, sSSDB, sConfirm, sRecurse, bRecurse
Dim oFs, oVSSDB, oVSSItem, oShell, oEnv, oExec

'''''' CONSTANTS ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
sSSPath     = "\\phsraga\development_vss$\"

'''''' INITIALIZE OBJECTS '''''''''''''''''''''''''''''''''''''''''''''''''''''
Set oShell  = CreateObject("WScript.Shell")
Set oEnv    = oShell.Environment("PROCESS")
Set oFs     = CreateObject("Scripting.FileSystemObject")
Set oVSSDB  = CreateObject("SourceSafe")

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Instr(1, LCase( WScript.FullName ), "wscript.exe") > 0 Then 
   WScript.Echo "This Script can only be run with cscript.exe"
   WScript.Quit
End If

' Get the Source Safe Location
WScript.Echo "Please select Source Safe location"
WScript.Echo "1 C:\Program Files\Microsoft Visual SourceSafe\SS.EXE"
WScript.Echo "2 Enter My Own Path"
WScript.StdOut.Write "Source Safe Location (1-3): "
sSSEXEPath = WScript.StdIn.ReadLine

Select Case sSSEXEPath
   Case 1
      sSSEXEPath = "C:\Program Files\Microsoft Visual SourceSafe\SS.EXE"
   Case 2
      WScript.StdOut.Write "Enter path (no quotes): "
      sSSEXEPath = WScript.StdIn.ReadLine
   Case Else
      WScript.Echo "Please enter 1 - 2.  Try again."
      WScript.Quit
End Select

' Check for existence of Source Safe Exe
If Not oFs.FileExists ( sSSEXEPath ) Then
   WScript.Echo "Source Safe Executable doesn't exist in path specified.  Try again."
   WScript.Quit
End If

' Check for existance of srcsafe.ini
If Not oFs.FileExists ( sSSPath & "\" & sSSDB & "\srcsafe.ini" ) Then
   WScript.Echo "Weird.  Seems like the Database specified doesn't exist.  "
   WScript.Quit
Else
   ' Set ssdir Environment Variable for Source Safe Command Line
   oEnv("ssdir")  = sSSPath & sSSDB 
   oVSSDB.Open sSSPath & "\" & sSSDB & "\srcsafe.ini"
End If

' Get the starting project
WScript.Echo
WScript.Echo "Please enter starting project"
WScript.StdOut.Write "Project: $/"
sStartProject  = "$/" & WScript.StdIn.ReadLine

Set oVSSItem = oVSSDB.VSSItem ( sStartProject, False )
If Err.number <> 0 Then
   WScript.Echo "Project: " & sStartProject & " doesn't exist in " & sSSDB & ".  Try again."
   'WScript.Echo Err.description
   WScript.Quit
End If

' Recurse?
WScript.Echo
WScript.Echo "Recurse projects?"
WScript.StdOut.Write "Recurse (y/n): "
sRecurse = WScript.StdIn.ReadLine

Select Case LCase( sRecurse )
   Case "y"
      bRecurse = True
      sRecurse = " recursively"
   Case "n"
      bRecurse = False
      sRecurse = ""
   Case Else
      WScript.Echo "Please enter y or n.  Try again."
      WScript.Quit
End Select

WScript.Echo
WScript.Echo

RecurseProject oVSSItem

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RecurseProject ( oProject )

   Dim oItem, oVSSCheckout, sUserName
   On Error Resume Next

   If bRecurse Then

      For Each oItem In oProject.Items
         If oItem.Type = 0 Then 
            ' Item is a Project
            RecurseProject oItem
         Else
            WScript.Echo oItem.Name
            Set oExec = oShell.Exec ( """" & sSSEXEPath & """ Diff -I- -IEW -DS180 -O@Diffs.txt """ & oProject.Spec & "/" & oItem.Name & """ """ & oItem.LocalSpec & """" )
            Do While oExec.Status = 0
               WScript.Sleep 1000
            Loop
         End If
      Next
   End If

End Sub

Set oFs           = Nothing
Set oVSSDB        = Nothing
Set oVSSItem      = Nothing
Set oShell        = Nothing
Set oEnv          = Nothing
Set oExec         = Nothing
'Set oItem         = Nothing
'Set oVSSCheckout  = Nothing